#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include "list.h"
#include "log.h"
#include "yapiparser.h"
#include "enprodev.h"


static char* _avg_str(const char * code, const char* str)
{
    char* avg = NULL;
    int sublen = strlen(str) + 2;
    char* pos = strstr(code, "code");
    pos = strchr(pos, '_');
    if ( pos )
    {
        char* start = pos += 1;
        char* end = strchr(start, '-');
        if ( end )
        {
            int len = end - start;
            avg = (char*)malloc(len + sublen);
            if ( avg )
            {
                strncpy(avg, start, len);
                avg[len] = '\0';
                strcat(avg, "-");
                strcat(avg, str);
                //printf("code: %s\n", avg);
            }
        }
        else
        {
            LOG_E("Does not contain the character - ...\n");
        }
    }
    else
    {
        LOG_E("Not found in str-code have -- code ...\n");
    }
    
    return avg;
}

static char* _mean_str(const char * code)
{
    char* mean = NULL;
    char* pos = strstr(code, "mean");

    if ( pos )
    {
        char* start = pos += 7;
        char* end = strchr(start, '\"');
        if ( end )
        {
            int len = end - start;
            //printf("mean length: %d\n", len);
            mean = (char*)malloc(len + 1);
            if ( mean )
            {
                strncpy(mean, start, len);
                mean[len] = '\0';
            }
        }
    }

    return mean;
}

//static int totoal_mean = 0;
static size_t yapiparser_analysis(void *buffer, size_t size, size_t nmemb, struct list_head *list)
{
    char* str = (char*)buffer;
    //printf("%s\n", buffer);
    char* avg = _avg_str(str, "Avg");
    char* mean = _mean_str(str);
    if ( mean && avg)
    {
        EnvProNode* node = (EnvProNode*)malloc(sizeof(EnvProNode));
        int len = strlen(mean) + strlen(avg) + 3;
        char* datstr = (char*)malloc(len);
        if ( node && datstr)
        {
            len = strlen(avg);
            strncpy(datstr, avg, len);
            datstr[len] = '\0';
            strcat(datstr, "=");
            strcat(datstr, mean);
            strcat(datstr, ";");
            //printf("%s\n", avg);
            node->len = strlen(datstr);
            node->avg = datstr;
            //printf("%s\n", datstr);
            list_add_tail(&node->list, list);
        }
        else
        {
            if ( node ) free(node);
            if (datstr) free(datstr);
        }

        //printf("parser: %p  ave: %s  mean: %s\n", node, node->avg, node->mean);

        //printf("epd str: %s\n", datstr);
    }
    
    if ( avg ) free(avg);
    if ( mean ) free(mean);
        
    //printf("yapiparser_analysis end\n");
    return (size * nmemb);
}


YapiParser* yapiparser_create(void)
{
    YapiParser* parser = (YapiParser*)malloc(sizeof(YapiParser));
    
    if ( parser )
    {
        parser->analysis = yapiparser_analysis;
    }

    return parser;
}

YapiParser* yapiparser_destroy(YapiParser* parser)
{
    if ( parser ) free(parser);

    return NULL;
}